<?php
function getOS($ins){
    $os_platform    =   "Unknown OS Platform";
    $os_array       =   array(
                            '/windows nt 6.2/i'     =>  'Windows 8',
                            '/windows nt 6.1/i'     =>  'Windows 7',
                            '/windows nt 6.0/i'     =>  'Windows Vista',
                            '/windows nt 5.2/i'     =>  'Windows Server 2003/XP x64',
                            '/windows nt 5.1/i'     =>  'Windows XP',
                            '/windows xp/i'         =>  'Windows XP',
                            '/windows nt 5.0/i'     =>  'Windows 2000',
                            '/windows me/i'         =>  'Windows ME',
                            '/win98/i'              =>  'Windows 98',
                            '/win95/i'              =>  'Windows 95',
                            '/win16/i'              =>  'Windows 3.11',
                            '/macintosh|mac os x/i' =>  'Mac OS X',
                            '/mac_powerpc/i'        =>  'Mac OS 9',
                            '/linux/i'              =>  'Linux',
                            '/ubuntu/i'             =>  'Ubuntu',
                            '/iphone/i'             =>  'iPhone',
                            '/ipod/i'               =>  'iPod',
                            '/ipad/i'               =>  'iPad',
                            '/android/i'            =>  'Android',
                            '/blackberry/i'         =>  'BlackBerry',
                            '/webos/i'              =>  'Mobile'
                        );

    foreach ($os_array as $regex => $value){
        if (preg_match($regex, $ins)){
            $os_platform    =   $value;
        }
    } 
    return $os_platform;
}

function getBrowser($ins) {
    $browser        =   "Unknown Browser";
    $browser_array  =   array(
                            '/msie/i'       =>  'Internet Explorer',
                            '/firefox/i'    =>  'Firefox',
                            '/safari/i'     =>  'Safari',
                            '/chrome/i'     =>  'Chrome',
                            '/opera/i'      =>  'Opera',
                            '/netscape/i'   =>  'Netscape',
                            '/maxthon/i'    =>  'Maxthon',
                            '/konqueror/i'  =>  'Konqueror',
                            '/mobile/i'     =>  'Handheld Browser'
                        );

    foreach ($browser_array as $regex => $value) { 
        if (preg_match($regex, $ins)) {
            $browser    =   $value;
        }
    }
    return $browser;
}


function logs($action,$data=''){
	//log_action (edit, remove, delete, login, logout)
	$location = $_SERVER["REMOTE_ADDR"]; // ip?
	$user_agent = $_SERVER['HTTP_USER_AGENT']; // browser
	$data = "(".getBrowser($user_agent)." on ".getOS($user_agent).") - ".$user_agent; // os + browser
	
	$datax = array();
	if(isset($_SESSION['_skpd']))		$datax['skpd_nama'] = $_SESSION['_skpd'];
	if(isset($_SESSION['_idakun']))		$datax['user_akun'] = $_SESSION['_idakun'];
	if(isset($_SESSION['_username']))	$datax['user_name'] = $_SESSION['_username'];
	if(isset($_SESSION['_iduslevel']))	$datax['user_level'] = $_SESSION['_iduslevel'];
	
	$dataz = get_data("select uslevel from user_level where id_uslevel=".$datax['user_level']);
	$datax['user_level'] = $dataz['uslevel'];
	
	$userid = $datax['user_akun'];
	$username = $datax['user_name']." (".$datax['user_level'].")";
	$id = get_maxid("log_id","mus_logs");
	exec_query("insert into mus_logs(log_id,log_timestamp,log_userid,log_username,log_skpd,log_remote_ip,log_browser,log_action,log_data) values(".$id.",NOW(),'".$userid."','".$username."','".$datax['skpd_nama']."','".$location."','".$data."','".$action."','".$data."')");
}
?>